﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ivony.Data;
using System.Data;

namespace XCommunity.Forums.Extension
{
  internal class DataProvider
  {

    public static SqlDbUtility DbUtility = new SqlDbUtility( @"Data Source=(local);Initial Catalog=XCommunity;Integrated Security=True" );


    public CachedCounter LoadVisits( int topicId )
    {
      DataRow dataItem = DbUtility.ExecuteSingleRow( "SELECT Visits FROM TopicFrameMetas WHERE ID = {0}", topicId );


      if ( dataItem == null )
      {
        DbUtility.ExecuteNonQuery( "INSERT INTO TopicFrameMetas ( ID, Visits ) VALUES ( {...} )", topicId, 0 );
        return new CachedCounter( 0 );
      }
      else
        return new CachedCounter( (int) dataItem["Visits"] );
    }

    public void UpdateVisits( int topicId, CachedCounter visits )
    {
      DbUtility.ExecuteNonQuery( "UPDATE TopicFrameMetas SET Visits = Visits + {1} WHERE ID = {0}", topicId, visits.Reset() );
    }


  }
}
